const { Model, DataTypes } = require("sequelize");

class OrdersPay extends Model {
  static init(sequelize) {
    super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        orderNum: {
          type: DataTypes.STRING(30),
          allowNull: false,
        },
        user_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
          references: {
            model: "users", // 关联 users 表
            key: "id", // 关联 users 表的 id 字段
          },
        },
        type: {
          type: DataTypes.STRING(10),
          allowNull: false,
        },
        des: {
          type: DataTypes.STRING(50),
        },
        amount: {
          type: DataTypes.FLOAT,
          allowNull: false,
        },
        createdAt: {
          type: DataTypes.DATE,
          defaultValue: DataTypes.NOW, // 默认值为当前时间
        },
      },
      {
        sequelize,
        modelName: "OrdersPay",
        tableName: "orders_pay",
        timestamps: false, // 禁止自动管理 createdAt 和 updatedAt
      }
    );
  }

  static associate(models) {
    // 定义关联关系
    this.belongsTo(models.Users, {
      foreignKey: "user_id",
      as: "user",
    });
  }
}

module.exports = OrdersPay;
